﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using OgilvyOne.Model;
using OgilvyOne.IDAL;
using OgilvyOne.DALFactory;

namespace OgilvyOne.BLL
{
    public class LoginBll
    {
        public const string LOGIN_USER_KEY = "LOGIN_USER_KEY";
        public const string LOGIN_USER_POPEDOM_KEY = "LOGIN_USER_POPEDOM_KEY";

        /// <summary>
        /// 后台用户登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public bool login(string username, string password, System.Web.SessionState.HttpSessionState session)
        {
            UsersModel user = LoginFactory.create().login(username, password);

            if (user != null)
            {
                session.Add(LOGIN_USER_KEY, user);
                if (!isDynamicPopedom(session))
                {//设置用户权限
                    Hashtable map = RolePopedomFactory.create().getUserPopedomMenu(user.Userrole);
                    Hashtable pmap = new Hashtable();
                    foreach (string key in map.Keys)
                    {//加入父级基本权限,保证菜单级联
                        string[] idstr = key.ToString().Split(',');
                        string svalue = "";
                        if (idstr.Length > 2)
                        {
                            svalue = idstr[0] + ',' + idstr[1];
                            if (!map.ContainsKey(svalue)&&!pmap.ContainsKey(svalue))
                            {
                                pmap.Add(svalue, "");
                            }
                            for (int z = 2; z < idstr.Length - 1; z++)
                            {
                                svalue = svalue + "," + idstr[z];
                                if (!map.ContainsKey(svalue) && !pmap.ContainsKey(svalue))
                                {
                                    pmap.Add(svalue,"");
                                }
                            }
                        }
                    }
                    foreach (string key in pmap.Keys)
                    {
                        map.Add(key, "base");
                    }
                    session.Add(LOGIN_USER_POPEDOM_KEY, map);
                }
                return true;
            }

            return false;

        }
        public static readonly string dp = OgilvyOne.Utility.ConfigUtil.GetStringValue("DynamicPopedom");
        /// <summary>
        /// 是否权限设置或超级管理员
        /// </summary>
        /// <returns></returns>
        public bool isDynamicPopedom(System.Web.SessionState.HttpSessionState session)
        {
            if (dp == "0") return true;
            UsersModel user = (UsersModel)session[LOGIN_USER_KEY];
            if (("," + user.Userrole + ",").IndexOf(",1,") > -1)
            {//角色1 为超级管理员
                return true;
            }
            return false;
        }

        public const string COLUMN_TYPE_SYSTREE = "systree";
        /// <summary>
        /// 获取管理栏目
        /// </summary>
        /// <param name="type">栏目类型 (systree 系统)</param>
        /// <returns></returns>
        public IList getColumnList(string type)
        {
            if (type == COLUMN_TYPE_SYSTREE)
            {
                return LoginFactory.create().getSysColumnList();
            }
            else
            {
                return LoginFactory.create().getColumnList();
            }
        }


    }
}
